package com.example.forestapp

import android.content.Intent
import android.os.Bundle
import android.widget.SearchView
import androidx.appcompat.app.AppCompatActivity
import com.amap.api.maps.AMap
import com.amap.api.maps.MapView
import com.example.forestmanagement.R
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.bottomsheet.BottomSheetDialog

class MainActivity : AppCompatActivity() {
    private lateinit var mapView: MapView
    private lateinit var aMap: AMap
    private lateinit var bottomNavigation: BottomNavigationView
    private lateinit var searchView: SearchView
    private lateinit var drawToolsDialog: BottomSheetDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mapView = findViewById(R.id.map_view)
        mapView.onCreate(savedInstanceState)
        aMap = mapView.map

        bottomNavigation = findViewById(R.id.bottom_navigation)
        searchView = findViewById(R.id.searchView)

        setupBottomNavigation()
        setupSearchView()
        setupDrawTools()
    }

    private fun setupBottomNavigation() {
        bottomNavigation.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.navigation_map -> true
                R.id.navigation_marks -> {
                    startActivity(Intent(this, MarkManagementActivity::class.java))
                    true
                }
                R.id.navigation_draw -> {
                    drawToolsDialog.show()
                    true
                }
                R.id.navigation_command -> {
                    startActivity(Intent(this, CommandCenterActivity::class.java))
                    true
                }
                else -> false
            }
        }
    }

    private fun setupSearchView() {
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                startActivity(Intent(this@MainActivity, HistoryDataActivity::class.java))
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return false
            }
        })
    }

    private fun setupDrawTools() {
        drawToolsDialog = BottomSheetDialog(this)
        drawToolsDialog.setContentView(R.layout.layout_draw_tools)
    }

    override fun onResume() {
        super.onResume()
        mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView.onDestroy()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapView.onSaveInstanceState(outState)
    }
} 